/**
 * login 登录页数据
 */

import { getShopListAjax, deleteShopAjax, checkedShopAjax } from '@/api/shopTrolley'
export default {
  namespaced: true,
  state: {
    cartList: [],
    cartTotal: []
  },
  mutations: {
    commitShopData (state, result) {
      console.log(state)
      state.cartList = result.cartList
      state.cartTotal = result.cartTotal
    }

  },
  actions: {
    getShopData ({ commit }) {
      getShopListAjax().then(res => {
        console.log(res)
        if (res.errno === 501) {
          console.log(this.$router)
          this._vm.$route.replace({ name: 'Login' })
        }
        commit('commitShopData', res.data)
      })
    },
    deleteShopData ({ commit }, id) {
      deleteShopAjax({ productIds: [id] }).then(res => {
        commit('commitShopData', res.data)
      })
    },
    checkShopData ({ commit }, { productIds, isChecked }) {
      console.log(productIds, isChecked)
      checkedShopAjax({ productIds, isChecked }).then(res => {
        console.log(res)
        commit('commitShopData', res.data)
      })
    }
  },
  getters: {
    getCartList (state) {
      return state.cartList
    },
    getCartTotal (state) {
      return state.cartTotal
    },
    getCheckIds (state) {
      const checkIds = []
      state.cartList.forEach(item => {
        if (item.checked) {
          checkIds.push(item.productId)
        }
      })
      console.log(checkIds)
      return checkIds
    }

  }
}
